package readini

import (
	"fmt"
)

//Configuration all section in the ini file
type Configuration struct {
	SectionList []string
	Sections    map[string]*Section
}

// init initialize
func (cfg *Configuration) init() {
	cfg.Sections = make(map[string]*Section)
}

//newSection create a new section
func (cfg *Configuration) newSection(sectionName string) error {
	if len(sectionName) == 0 {
		return fmt.Errorf("Empty section name")
	}
	if cfg.Sections[sectionName] != nil {
		return fmt.Errorf("Section[%v] already exists", sectionName)
	}

	cfg.SectionList = append(cfg.SectionList, sectionName)
	cfg.Sections[sectionName] = &Section{
		Values: make(map[string]string),
	}

	return nil
}

//GetSection return section according to section name
func (cfg *Configuration) GetSection(sectionName string) (*Section, error) {
	if len(sectionName) == 0 {
		sectionName = DefaultSection
	}
	sec := cfg.Sections[sectionName]
	if sec == nil {
		return nil, fmt.Errorf("Section[%v] does not exist", sectionName)
	}
	return sec, nil
}
